package cn.gov.pbc.gd.credit.web.controller.inquiry;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liu.jbase.BaseVo;
import com.liu.jbase.ResultVo;
import com.liu.jbase.controller.impl.BaseControllerImpl;

import cn.gov.pbc.gd.credit.api.realuser.IRealUserApi;
import cn.gov.pbc.gd.credit.api.user.IUserApi;
import cn.gov.pbc.gd.credit.center.service.user.ICCUserService;
import cn.gov.pbc.gd.credit.common.constant.CodeConstant;
import cn.gov.pbc.gd.credit.common.constant.ConfConstant;
import cn.gov.pbc.gd.credit.common.vo.LoginVo;
import cn.gov.pbc.gd.credit.core.service.inquiry.IChargeService;
import cn.gov.pbc.gd.credit.core.service.inquiry.IInquiryService;
import cn.gov.pbc.gd.credit.user.service.dictionary.IDictionaryService;
import cn.gov.pbc.gd.credit.vo.center.ReqCCUserVo;
import cn.gov.pbc.gd.credit.vo.center.ResCCUserVo;
import cn.gov.pbc.gd.credit.vo.center.charge.ReqCCChargeVo;
import cn.gov.pbc.gd.credit.vo.center.charge.ResCCChargeVo;
import cn.gov.pbc.gd.credit.vo.center.query.ReqCCCreditReportVo;
import cn.gov.pbc.gd.credit.vo.center.query.ReqSecondStepVo;
import cn.gov.pbc.gd.credit.vo.center.query.ResCCApplyTblVo;
import cn.gov.pbc.gd.credit.vo.center.query.ResCCCreditReportVo;
import cn.gov.pbc.gd.credit.vo.center.query.ResCCJusticeVo;
import cn.gov.pbc.gd.credit.vo.dictionary.DictionaryVo;
import cn.gov.pbc.gd.credit.vo.inquiry.InquiryVo;
import cn.gov.pbc.gd.credit.vo.inquiry.ResInquiryVo;
import cn.gov.pbc.gd.credit.vo.inquiry.agent.AgentVo;
import cn.gov.pbc.gd.credit.vo.inquiry.apply.ApplyTblVo;
import cn.gov.pbc.gd.credit.vo.inquiry.apply.ResApplyTblVo;
import cn.gov.pbc.gd.credit.vo.inquiry.justice.JusticeVo;
import cn.gov.pbc.gd.credit.vo.realuser.ResRealUserVo;
import cn.gov.pbc.gd.credit.vo.user.ResUserVo;
import cn.gov.pbc.gd.credit.vo.user.UserVo;

@Controller
@RequestMapping("/inquiry")	
public class InquiryController extends BaseControllerImpl<ResInquiryVo, InquiryVo, IInquiryService> {
  
	@Autowired
	private IInquiryService inquiryService;
	
	@Autowired
	private IChargeService chargeService;
	
	@Autowired
	private IDictionaryService dictionaryService;
	
	@Autowired
	private ICCUserService ccUserService;
	
	@Autowired
	private IRealUserApi realUserApi;
	
	@Autowired
	private IUserApi userApi;
	@RequestMapping("/person")
	public String person(InquiryVo vo, Model model, HttpServletRequest request){
		DictionaryVo dictionaryVo = new DictionaryVo();
		dictionaryVo.setKeyword("FACE_RATIO");
		dictionaryService.getDicByKeyword(dictionaryVo, request);
		model.addAttribute("faceRatio", dictionaryService.getDicByKeyword(dictionaryVo, request).getData().getDicValue());
		model.addAttribute("limitQueryFlag", inquiryService.checkLimitQuery(vo, request).getData());
		return "inquiry/person";
	}
	
	@RequestMapping("/agent")
	public String agent(AgentVo vo, Model model, HttpServletRequest request){
		
		model.addAttribute("limitQueryFlag", inquiryService.checkLimitQuery(vo, request).getData());
		return "inquiry/agent";
	}
	
	@RequestMapping("/justice")
	public String justice(JusticeVo vo, Model model, HttpServletRequest request){
		
		return "inquiry/justice";
	}

	@RequestMapping("aprint")
	public String againPrint(ReqCCCreditReportVo vo, Model model, HttpServletRequest request){

		return "inquiry/aprint";
	}

	@ResponseBody
	@RequestMapping("apply/table")
	public ResultVo<ResCCApplyTblVo> applyTable(ReqSecondStepVo vo, HttpServletRequest request){

		return inquiryService.toApplyTbl(vo, request);
	}
	
	@ResponseBody
	@RequestMapping("credit/report")
	public ResultVo<ResCCCreditReportVo> creditReport(ReqCCCreditReportVo vo, HttpServletRequest request){
		
		return inquiryService.creditReport(vo, request);
	}
	
	@ResponseBody
	@RequestMapping("again/report")
	public ResultVo<ResCCCreditReportVo> againReport(ReqCCCreditReportVo vo, HttpServletRequest request){

		return inquiryService.againPrintReport(vo, request);
	}

	@ResponseBody
	@RequestMapping("credit/print")
	public ResultVo<ResInquiryVo> creditPrint(InquiryVo vo, HttpServletRequest request){
		
		return inquiryService.printReport(vo, request);
	}
	
	@ResponseBody
	@RequestMapping("justice/save")
	public ResultVo<ResCCJusticeVo> justiceSave(JusticeVo vo, HttpServletRequest request){
		
		return inquiryService.addJustice(vo, request);
	}
	
	@ResponseBody
	@RequestMapping("agent/save")
	public ResultVo<ResInquiryVo> agentSave(AgentVo vo, HttpServletRequest request){
		
		return inquiryService.addAgent(vo, request);
	}
	
	@RequestMapping("/device")
	public String device(AgentVo vo, Model model, HttpServletRequest request){
		
		return "inquiry/device";
	}
	
	@ResponseBody
	@RequestMapping("charge")
	public ResultVo<ResCCChargeVo> charge(ReqCCChargeVo vo, HttpServletRequest request){
		
		return chargeService.charge(vo, request);
	}
	
	@ResponseBody
	@RequestMapping("charge/cancel")
	public ResultVo<ResCCChargeVo> chargeCancel(ReqCCChargeVo vo, HttpServletRequest request){
		
		return chargeService.cancelCharge(vo, request);
	}
	
	@ResponseBody
	@RequestMapping("update/apply/tbl")
	public ResultVo<ResApplyTblVo> updateApplyTbl(ApplyTblVo vo, HttpServletRequest request){
		
		return inquiryService.updateApplyTbl(vo, request);
	}
	
	@ResponseBody
	@RequestMapping("find/apply/tbl")
	public ResultVo<ResApplyTblVo> findApplyTbl(ApplyTblVo vo, HttpServletRequest request){
		
		return inquiryService.findApplyTbl(vo, request);
	}
	
	@RequestMapping("/person/step5")
	public String person(HttpServletRequest request){
		
		return "inquiry/person/step5";
	}
	/**
	 * 异议处理
	 * @param vo
	 * @param request
	 * @param httpSession
	 * @param baseVo
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("objectionDeal")
	public String objectionDeal(LoginVo vo, HttpServletRequest request,HttpSession httpSession,Model model) throws ServletException, IOException{
		ResultVo<ResRealUserVo> resultVo = new ResultVo<>();
		if(httpSession.getAttribute("account")==null||httpSession.getAttribute("account")==""){
			return "redirect:/login";
		}
		String account=httpSession.getAttribute("account").toString();
		UserVo userVo = new UserVo();
		userVo.setAccount(account);
		ResUserVo resUserVo = userApi.query(userVo);
		if(resUserVo.getRealUserId()==null||resUserVo.getRealUserId()==""||"".equals(resUserVo.getRealUserId())){
			model.addAttribute("message", "当前用户没有关联征信中心用户，无法查看异议处理");
			return "inquiry/objectionDeal";
		}
		ResRealUserVo resRealUserVo = realUserApi.queryById(resUserVo.getRealUserId());
		ReqCCUserVo loginVo = new ReqCCUserVo();
		loginVo.setPassword(resRealUserVo.getUserPwd());
		loginVo.setUserid(resRealUserVo.getAccount());
//		ResultVo<ResCCUserVo> resultLogin = ccUserService.login(loginVo, request);
//		if(CodeConstant.SUCCESS.equals(resultLogin.getCode())){
//			model.addAttribute("message", "success");
//			model.addAttribute("requestUrl",ConfConstant.ccObjectionDeal);
//		}else{
//			model.addAttribute("message", resultLogin.getMsg());
//		}
        request.setAttribute("userId",resRealUserVo.getAccount());
		request.setAttribute("password",resRealUserVo.getUserPwd());
//		request.setAttribute("loginVo",loginVo);
		return "inquiry/objectionDeal";
	}
}
